const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa2-cors");// 允许跨域
app.use(cors());
app.use(parser());
let fruitList = ["香蕉",    "苹果","鸭梨","西瓜"];
router.get("/fruits", async ctx => {
    ctx.body = fruitList;
})
router.post("/fruits",async ctx=>{ 
    let fruit = ctx.request.body.fruit;
    fruitList.push(fruit);
    ctx.body = true;
})
router.delete("/fruits/:index", async ctx => {
    let index = ctx.params.index;
    fruitList.splice(index,1);
    ctx.body = true;
})
router.put("/fruits/:index", async ctx => {
    let index = ctx.params.index;
    let setFruit = ctx.request.body.setFruit;
    fruitList.splice(index,1,setFruit);
    ctx.body = true;
})
app.use(router.routes());
app.listen(3000,()=>{
    console.log("3000")
})